<?php
/**
 * 合作加盟
 */
class Controller_Partner extends Controller_Abstract
{
    function actionIndex(){
    	if ( (int)$_SESSION['account_arr']['chk_uin'] > 0 ){ //是否已经登陆
    		return $this->_redirect( url('partner/actor') );
    	}else return $this->_redirect( url('login/index', array('redirect'=>url(''))) );
    }//END func Index
    
	//演绎者签约
    function actionActor(){
    	$this->_view['role'] = 'actor';
    	if ( (int)$_SESSION['account_arr']['chk_uin'] > 0 ){ //是否已经登陆
    		$this->_context->uin = $_SESSION['account_arr']['chk_uin'];
    	}else return $this->_redirect( url('login/index', array('redirect'=>url(''))) );
    	
    	if( empty($_POST['kkyoo_add_actor']) ){
    		$this->_viewname = 'actor_license';
    	}{
    		// 默认...设置
	    	$attrs = array('uin','name','id_card','info','pic_url','address','phone','qq','bank','bank_card','sex','age','show_age','province','city');	
    		$p = $_POST;
    		foreach($attrs as $k){
    			if( $k == 'province' and empty($this->_context->province) )
    				$this->_view['province'] = '浙江';
    			elseif( $k == 'city' and empty($this->_context->city) )
    				$this->_view['city'] = '杭州';
    			else
    				$this->_view[$k] = $this->_context->$k;
    		}
    		$this->_view['error'] = null;
    		$this->_view['finish'] = 0;
    		
    		if($this->_context->mark == 'create'){
    			$this->_view['error'] = $this->is_actor( 'actor' );
    			if( empty($this->_view['error']) ){
	    			//开始图片上传
	    			$this->_view['error'] = $this->uploadImage();
	    			if( empty($this->_view['error']) ){
	    				//写入艺人信息表
	    				$this->_view['error'] = $this->add();
	    				if( empty($this->_view['error']) )  $this->_view['finish'] = 1;
	    			}
    			}
    		}//END save
    	}
    }//END func Actor
    
	//销售商申请
    function actionSeller(){
    	$this->_view['role'] = 'seller';
    	if ( (int)$_SESSION['account_arr']['chk_uin'] > 0 ){ //是否已经登陆
    		$this->_context->uin = $_SESSION['account_arr']['chk_uin'];
    	}else return $this->_redirect( url('login/index', array('redirect'=>url(''))) );
    	
    	if( empty($_POST['kkyoo_add_seller']) ){
    		$this->_viewname = 'seller_license';
    	}{
    		// 默认...设置
	    	$attrs = array('uin','name','id_card','info','pic_url','address','phone','qq','bank','bank_card','sex','age','show_age','province','city','type');	
    		$p = $_POST;
    		foreach($attrs as $k){
    			if( $k == 'province' and empty($this->_context->province) )
    				$this->_view['province'] = '浙江';
    			elseif( $k == 'city' and empty($this->_context->city) )
    				$this->_view['city'] = '杭州';
    			else
    				$this->_view[$k] = $this->_context->$k;
    		}
    		$this->_view['error'] = null;
    		$this->_view['finish'] = 0;
    		
    		if($this->_context->mark == 'create'){
    			$this->_view['error'] = $this->is_actor('seller');
    			if( empty($this->_view['error']) ){
    				//开始图片上传
	    			$this->_view['error'] = $this->uploadImage();
	    			if( empty($this->_view['error']) ){
	    				//写入艺人信息表
	    				$this->_view['error'] = $this->add();
	    				if( empty($this->_view['error']) )  $this->_view['finish'] = 1;
	    			}
    			}
    		}//END save
    	}
    }//END func Actor
    
    //匹配演绎者
    function is_actor( $type ){
    	if( empty($this->_context->uin) ) return "K秀账号错误！";
    	$table =QDB::getConn();
    	
    	$sql1 = "SELECT COUNT(uin) AS is_kkyoo FROM kexoo_im.basic_tbl WHERE uin = '{$this->_context->uin}' ";
    	$handel = $table->execute($sql1);
    	$re1 = $handel->fetchRow();
    	$handel->free();
    	if( $re1['is_kkyoo'] != '1' ) return "不存在K秀账号({$this->_context->uin})！";
    	
    	$sql2 = " SELECT COUNT(uin) AS is_actor FROM kkyoo_rooms.tbl_rooms_actor WHERE uin = '{$this->_context->uin}' AND type = '{$type}' ";
    	$handel = $table->execute($sql2);
    	$re2 = $handel->fetchRow();
    	$handel->free();
    	if( (int)$re2['is_actor'] > 0 ) return "K秀账号({$this->_context->uin})已经存在, 请不要重复提交申请！";
    	else return null;
    }
    
    //演绎者签约
    function add(){
   	  try {
    	$actor = new Partner();
    	$actor->uin = $this->_context->uin; //用户ID
    	$actor->name = $this->_context->name; 
    	$actor->id_card = $this->_context->id_card; //省份证
    	$actor->info = $this->_context->info; //个人简介
    	$actor->pic_id = $this->_context->pic_id; //照片ID
    	$actor->pic_url = $this->_context->pic_url; //图片路径
    	$actor->address = $this->_context->address;
    	$actor->phone = $this->_context->phone;
    	$actor->qq = $this->_context->qq;
    	$actor->bank = $this->_context->bank;
    	$actor->bank_card = $this->_context->bank_card;
    	$actor->sex = $this->_context->sex;
    	$actor->age = $this->_context->age;
    	if( empty($this->_context->show_age) )
    		$actor->show_age = '0';
    	else $actor->show_age = '1';

    	$actor->province = $this->_context->province;
    	$actor->city = $this->_context->city;
    	$actor->status = 0;
    	$actor->type = $this->_context->type;
    	
    	$actor->save(99, 'create');
    	return null;
   	  }catch(Exception $e){
   	  	return $e->getMessage();
   	  }
    }//END func actoradd
    
    
    //上传图片
    function uploadImage(){
		$message = '';
		if( empty($_FILES['file']['size']) ) return '必需上传个人照片!';
		
    	require_once dirname(dirname(__FILE__)).DS.'helper'.DS.'upload.func.php';
    	$path_rel = upFile('200x150');
    	if( $path_rel == 2 ) $message = '不支持的图片格式！';
    	elseif ( !empty($path_rel) ){
    		$path_abs = "http://www.kkyoo.com".$path_rel;
    		$uin = $this->_context->uin;
    		$updated = time();
    		
    		$table =QDB::getConn();
    		$sql = "INSERT INTO kkyoo_rooms.tbl_rooms_actor_images (uin,path_rel,path_abs,updated) "
    			." values('{$uin}', '{$path_rel}', '{$path_abs}', '{$updated}' ) ";
    		//dump( $sql );
    			
    		if( $table->execute($sql) ){
    			$handel = $table->execute("SELECT LAST_INSERT_ID() AS pic_id");
    			$re = $handel->fetchRow();
    			$handel->free();
    			$this->_context->pic_id = $re['pic_id'];
    			$this->_context->pic_url = $path_abs;
    		}else $message = '图片上传失败, 请重试！';
    	}
    	return $message;
    }
    
}//END class Partner